new Vue({
    el: '#apply',
    data: {
        applyList:[],
        timeList:[
            {time:"08:00-09:00",active:false},{time:"09:00-10:00",active:false},{time:"10:00-11:00",active:false},{time:"11:00-12:00",active:false},
            {time:"12:00-13:00",active:false},{time:"13:00-14:00",active:false},{time:"14:00-15:00",active:false},{time:"15:00-16:00",active:false},
            {time:"16:00-17:00",active:false},{time:"17:00-18:00",active:false},{time:"18:00-19:00",active:false},{time:"19:00-20:00",active:false},
            {time:"20:00-21:00",active:false}
        ],
        dateList:[],
        //查询回来的表单
        finalList:[],
        // 所有页面的数据
        totalPage: [],
        pageSize: 8,
        // 共几页
        pageNum: 1,
        // 当前显示的数据
        dataShow: "",
        // 默认当前显示第一页
        currentPage: 0,
        //楼层
        placestorey:"",
        //类型
        placetype:"",
        //选择的时间
        placeDate:[],
        //选择的时间段
        ftimeList:[]
    },
    filters: {
        formatDate: function (value) {
            let date = new Date(value);
            let y = date.getFullYear();
            let MM = date.getMonth() + 1;
            MM = MM < 10 ? ('0' + MM) : MM;
            let d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + MM + '-' + d ;
        }
    },
    methods:{
        getPlaceApply(){
            var list;
            $.ajax({
                // 设置json的属性和值
                url: "/researchApply",
                contentType: "application/json;charset=UTF-8",
                async:false,
                type: "Post",
                success: function (data) {
                    list = data;
                }
            })
            return list;
        },
        //查询对应时间
        researchA(){
            //获取选择的时间段
            this.timeList.forEach(v => {
                if (v.active) {
                    this.ftimeList.push(v.time)
                }
            })
            this.dateList.forEach(v => {
                if(v.active){
                    this.placeDate.push(v.date)
                }
            })
            var list1;
            var placetest = {};
            placetest.placestorey = this.placestorey
            placetest.placetype = this.placetype
            placetest.placeDate = this.placeDate
            placetest.ftimeList = this.ftimeList
            $.ajax({
                // 设置json的属性和值
                url: "/researchApply1",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(placetest),
                async:false,
                type: "post",
                success: function (data) {
                    list1 = data;
                    alert("查询成功")
                }
            })

            this.finalList = list1
            this.ftimeList = []
            this.placeDate = []
            this.currentPage = 0

            this.timeList.forEach(v => {
                v.active = false
            })
            this.dateList.forEach(v => {
                v.active = false
            })
        },
        toPlaceApply(placeapply,state){
            var bool = false
            if(state === 1){
                $.ajax({
                    // 设置json的属性和值
                    url: "/placeApplyAction",
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(placeapply),
                    async:false,
                    type: "post",
                    success: function (data) {
                        if(data===1) {
                            bool = true
                        }
                    }
                })
            }
            if(state === 0){
                console.log("hhsdashas")
                $.ajax({
                    // 设置json的属性和值
                    url: "/placeApplyAction1",
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(placeapply),
                    async:false,
                    type: "post",
                    success: function (data) {
                        if(data===1) {
                            bool = true
                        }
                    }
                })
            }
            // if(state===2){
            //     var a = "赛事待审核"
            //     console.log(a)
            //     placeapply.eventid = a
            //     $.ajax({
            //         // 设置json的属性和值
            //         url: "/placeApplyAction2",
            //         contentType: "application/json;charset=UTF-8",
            //         data: JSON.stringify(placeapply),
            //         async:false,
            //         type: "post",
            //         success: function (data) {
            //             if(data===1) {
            //                 bool = true
            //             }
            //         }
            //     })
            // }
            if(bool){
                let index = 0
                for(var i=0;i<this.finalList.length;i++){
                    if(this.finalList[i] === placeapply){
                        index = i
                    }
                }
                this.finalList.splice(index,1);
                alert("预约成功")
            }
        },
        turnPage(index){
          this.dataShow = this.totalPage[index]
          this.currentPage = index-1
        },
        // 下一页
        nextPage() {
            if (this.currentPage === this.pageNum - 1) return ;
            this.dataShow = this.totalPage[++this.currentPage];
        },
        // 上一页
        prePage() {
            if (this.currentPage === 0) return ;
            this.dataShow = this.totalPage[--this.currentPage];
        },
        //获取时间
        getDate(){
            var date = new Date();
            var list = this.getDateRange(date,8,false)
            return list
        },
        getDateRange(dateNow,intervalDays,bolPastTime){
            let oneDayTime = 24 * 60 * 60 * 1000;
            let list = [];
            let lastDay;
            if(bolPastTime == true){//前一周
                lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTime);
                list.push(this.formateDate(lastDay));
                list.push(this.formateDate(dateNow));
            }else{//后一周
                for(var i = 1;i<intervalDays;i++){
                    lastDay = new Date(dateNow.getTime() + i * oneDayTime);
                    list.push(this.formateDate(lastDay));
                }
            }
            return list;
        },
        formateDate(time){
            let year = time.getFullYear()
            let month = time.getMonth() + 1
            let day = time.getDate()
            if (month < 10) {
                month = '0' + month
            }
            if (day < 10) {
                day = '0' + day
            }
            return year + '-' + month + '-' + day + ''
        }
    },
    //刚进入页面时
    created(){
        // this.dateList = this.getDate()
        for (var i=0;i<this.getDate().length;i++){
            this.dateList.push({
                date:this.getDate()[i],
                active:false
            })
        }
        this.finalList = this.getPlaceApply()
    },
    computed:{
        //计算页数
        countPage(){
            this.pageNum = Math.ceil(this.finalList.length / this.pageSize) || 1;
            return this.pageNum
        },
        //每页显示数据
        showData(){
            for (let i = 0; i < this.pageNum; i++) {
                this.totalPage[i] = this.finalList.slice(this.pageSize * i, this.pageSize * (i + 1))
            }
            // 获取到数据后显示第一页内容
            this.dataShow = this.totalPage[this.currentPage];
            return this.dataShow
        }
    }
})